<html lang="en">
	<head>
		<title>three.js webgpu - depth texture</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info" class="invert">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Depth Texture</span>
			</div>

			<small>Rendering the scene's depth into a texture for later use</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { texture } from 'three/tsl';

			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

			let camera, scene, controls, renderer;

			let quad, renderTarget;

			const dpr = window.devicePixelRatio;

			init();

			function init() {

				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 20 );
				camera.position.z = 4;

				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x222222 );
				scene.overrideMaterial = new THREE.MeshBasicNodeMaterial();

				//

				const geometry = new THREE.TorusKnotGeometry( 1, 0.3, 128, 64 );

				const count = 50;
				const scale = 5;

				for ( let i = 0; i < count; i ++ ) {

					const r = Math.random() * 2.0 * Math.PI;
					const z = ( Math.random() * 2.0 ) - 1.0;
					const zScale = Math.sqrt( 1.0 - z * z ) * scale;

					const mesh = new THREE.Mesh( geometry );
					mesh.position.set(
						Math.cos( r ) * zScale,
						Math.sin( r ) * zScale,
						z * scale
					);
					mesh.rotation.set( Math.random(), Math.random(), Math.random() );
					scene.add( mesh );

				}

				//

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( dpr );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( animate );
				document.body.appendChild( renderer.domElement );

				const depthTexture = new THREE.DepthTexture();
				depthTexture.type = THREE.FloatType;

				renderTarget = new THREE.RenderTarget( window.innerWidth * dpr, window.innerHeight * dpr );
				renderTarget.depthTexture = depthTexture;

				window.addEventListener( 'resize', onWindowResize );

				// FX

				const materialFX = new THREE.MeshBasicNodeMaterial();
				materialFX.colorNode = texture( depthTexture );

				quad = new THREE.QuadMesh( materialFX );

				//

				controls = new OrbitControls( camera, renderer.domElement );
				controls.enableDamping = true;

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );
				renderTarget.setSize( window.innerWidth * dpr, window.innerHeight * dpr );

			}

			function animate() {

				renderer.setRenderTarget( renderTarget );
				renderer.render( scene, camera );

				renderer.setRenderTarget( null );
				quad.render( renderer );

			}

		</script>
	</body>
</html>
